
package com.ssm;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

class iCatched{
	public String envi = "";
	public String value = "";
	public String comp = "";
	public String opin = "";
}

public class IdeaBoxesActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	//tab flag value
	private final static int TAB1 = 0;
	private final static int TAB2 = 1;
	private final static int TAB3 = 2;
	private final static int TAB4 = 3;


	

	class iAdapter extends ArrayAdapter<iCatched> {

			List<iCatched> list;

			public iAdapter(Context context, int textViewResourceId,
					List<iCatched> list) {
				super(context, textViewResourceId, list);
				this.list = list;
			}

			@Override
			public View getView(final int position, View convertView,
					ViewGroup parent) {
				View view = convertView;
				if (view == null) {
					LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					view = vi.inflate(R.layout.list_row, null);
					
				}

//				TextView envi = (TextView) view.findViewById(R.id.envi);
//				TextView value = (TextView) view.findViewById(R.id.value);
//				TextView comp = (TextView) view.findViewById(R.id.comp);
//				TextView opin = (TextView) view.findViewById(R.id.opin);
//
//				
//				envi.setText(list.get(position).envi);
//				value.setText(list.get(position).value);
//				comp.setText(list.get(position).comp);
//				opin.setText(list.get(position).opin);

				return view;
			}
		}

	
	private DbManager dbM;
	
	private int flag; //tab select flag
	
	private ImageButton tab1;
	private ImageButton tab2;
	private ImageButton tab3;
	private ImageButton tab4;
	
	private EditText input;
	
	private Button add_btn;
	private Button voice_btn;
	
	private ImageButton refresh_btn;

	private TextView[] text = new TextView[10];
	private int[] text_id = { R.id.textView1,
			R.id.textView2,
			R.id.textView3,
			R.id.textView4,
			R.id.textView5,
			R.id.textView6,
			R.id.textView7,
			R.id.textView8,
			R.id.textView9,
			R.id.textView10	
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.idea_box_main);
        
        //layout Widgets Biding 
        tab1 = (ImageButton)findViewById(R.id.image_tab1);
        tab2 = (ImageButton)findViewById(R.id.image_tab2);
        tab3 = (ImageButton)findViewById(R.id.image_tab3);
        tab4 = (ImageButton)findViewById(R.id.image_tab4);
        
        input = (EditText)findViewById(R.id.edit_text);
        add_btn = (Button)findViewById(R.id.add_btn);
        voice_btn = (Button)findViewById(R.id.voice_btn);
        
        refresh_btn = (ImageButton)findViewById(R.id.refresh_btn);
        
        //ArrayList<String> relatedText = NaverAPI.Quary( "test" );
        
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);
        
        
        add_btn.setOnClickListener(this);
        voice_btn.setOnClickListener(this);
        
        refresh_btn.setOnClickListener(this);
        
        for(int i=0;i<10;i++){
        	text[i] = (TextView)findViewById(text_id[i]);
        	text[i].setOnClickListener(this);
        }
        

        //initialize Database
        dbM = new DbManager(this);
        
        Refresh();
        

    }
    public void Refresh(){
    	Random rnd = new Random();
        
    	switch(flag){
        case TAB1:
        	  for(int i=0;i<10;i++){
  	        	if(dbM.getKeyword1(i+1)!=null){
  	        		text[i].setText(dbM.getKeyword1(i+1));
  	        	}
  	        }
        	break;
        case TAB2:
        	 for(int i=0;i<10;i++){
 	        	if(dbM.getKeyword2(i+1)!=null)
 	        		text[i].setText(dbM.getKeyword2(i+1));
 	        }
        	break;
        case TAB3:
        	 for(int i=0;i<10;i++){
 	        	if(dbM.getKeyword3(i+1)!=null)
 	        		text[i].setText(dbM.getKeyword3(i+1));
 	        }
        	break;
        }
    }

    String[] strRelatedItems;
    boolean[] selected;
    	
	@Override
	public void onClick(View v) {
		
		switch(v.getId()){
		case R.id.image_tab1:
			flag = TAB1;
			break;
		case R.id.image_tab2:
			flag = TAB2;
			break;
		case R.id.image_tab3:
			flag = TAB3;
			break;
		case R.id.image_tab4:
			flag = TAB4;
			break;
			
		case R.id.voice_btn:
			
			String test = dbM.getKeyword1(3);
			Toast.makeText(this,test, Toast.LENGTH_SHORT).show();
			
		 	Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    	
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
						RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "입력하실 단어를 말씀하세요.");		
			intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
			startActivityForResult(intent, 17);
			
			break;
			
		case R.id.add_btn:
			
			final String inputText = input.getText().toString();
			ArrayList<String> relatedText = NaverAPI.Quary( inputText );
			
			strRelatedItems = new String[relatedText.size()];
			selected = new boolean[relatedText.size()];
			for(int i = 0; i < relatedText.size(); i++)
				strRelatedItems[i] = relatedText.get(i);
			
			
			AlertDialog.Builder adb = new AlertDialog.Builder(this);
		    adb.setTitle( inputText );
		    
		    adb.setPositiveButton("추가", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		            
		        	   addWordToDB( inputText );
		        	   for( int i = 0; i < selected.length; i++ ) {
			        		if( selected[i] )
			        			addWordToDB( strRelatedItems[i] );
		        	   }
		        	   Toast.makeText(IdeaBoxesActivity.this, "항목이 추가되었습니다.", Toast.LENGTH_SHORT).show();
		        	   
		         }
		    }
		      );
		    adb.setNegativeButton("취소", null);
		    adb.setMultiChoiceItems(strRelatedItems, selected, new DialogInterface.OnMultiChoiceClickListener() {
     
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                     // TODO Auto-generated method stub
                     selected[which] = isChecked;
      
               }
		    });
		    adb.show();
		   
			/*
			switch(flag){
			case TAB1:
				dbM.addKeyword1(input.getText().toString());
				input.setText("");
				break;
			case TAB2:
				dbM.addKeyword2(input.getText().toString());
				break;
			case TAB3:
				dbM.addKeyword3(input.getText().toString());
				break;
			case TAB4:
				break;
				
			}
			*/
			
			
		    
			break;
//		case R.id.voice_btn:
//			String test = dbM.getKeyword1(0);
//			Toast.makeText(this,test, Toast.LENGTH_SHORT).show();
//			break;
			
		}
		Refresh();
		
	}
	
	
	
	@Override
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	 {		
		
		if (requestCode == 17 && resultCode == RESULT_OK) 
		{  
			ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			// TODO Do something with the recognized voice strings		    
			
			String resultText = new String();
			for(int i = 0; i < results.size(); ++i)
				resultText += results.get(i); 
			input.setText( resultText );
		}
		super.onActivityResult(requestCode, resultCode, data);
	 }  
	
	private void selectWord()
	{
		String[] words = new String[3];

		// 현재 단어
		// 그 상자 이외의 것들랜덤으로 가져오기
		// random words
		words[0] = "환경";
		words[1] = "모바일게임";
		words[2] = "정육점";
		// 입력창
		
		Context mContext = getApplicationContext();
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.suggest_input,(ViewGroup) findViewById(R.id.SuggestInput));
		
		
		
		
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
	    adb.setTitle("3개의 조합 추천");
	    adb.setMessage( words[0] + " + " + words[1] + " + " + words[2] );
	    adb.setPositiveButton("추가", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {

	          }
	    }
	      );
	    
	    adb.setView(layout);
	    adb.setNegativeButton("취소", null);
	    adb.show();
	}
	
	
	private void addWordToDB(String word)
	{
		switch(flag){
		case TAB1:
			dbM.addKeyword1(word);
			input.setText("");
			break;
		case TAB2:
			dbM.addKeyword2(word);
			input.setText("");
			break;
		case TAB3:
			dbM.addKeyword3(word);
			input.setText("");
			break;
		case TAB4:
			break;
			
		}
	}
}